import { Window_Selectable } from "@/windows/Window_Selectable.js";

export class Window_SkillType extends Window_Selectable {
    constructor(width,height) {
        super(width,height,false);
        this.init(width,height);
    }

    init(width,height){
        this._actor = null;
        this._types=[];
        this.okHandler=null;
        this.cancelHandler=null;
        this.changedHandler=null;
        super.init(width,height);
    }

    destroy(opts){
        super.destroy(opts);
        this._actor=null;
        this._types.length=0;;
        this.okHandler=null;
        this.cancelHandler=null;
        this.changedHandler=null;
    }

    setHandler(name,callback){
        if(name=='ok'){
            this.okHandler=callback;
        }else if(name=='cancel'){
            this.cancelHandler=callback;
        }else if(name=='changed'){
            this.changedHandler=callback;
        }
    }

    row(){return this.length();}
    viewRow(){return 4;}
    length(){return this._types.length;}

    setActor(actor) {
        if (this._actor != actor) {
            this._actor = actor;
            this._types=this._actor.addedSkillTypes();
            this.refresh();
            this.activate();
        }
    }

    typeName(index){
        return $dataSystem.skillTypes[this._types[index]];
    }

    drawItem(index,x,y,width,height){
        this.drawText(this.typeName(index),x,y,width,height,'center');
    }

    refresh(){
        if(this._types){
            super.refresh();
        }
    }

    onResponse(index){
        this.okHandler?.(this._types[index],index);
    }

    onCancel(){
        this.cancelHandler?.();
    }

    onSelectChanged(index,oldIndex){
        this.changedHandler?.(this._types[index],this._types[oldIndex]);
    }
}